/*---------------------------------------------------------------------------
//
//	Copyright(C) SMIT Corporation, 2000-2010.
//
//  File	:	citag.h
//	Purpose	:	DVB-CI tag/structures, EN 50221, 1996
//	History :
//				2006-08-11 created by pmy.
//
---------------------------------------------------------------------------*/
#ifndef __CITAG_H
#define __CITAG_H

// Link Layer
#define LN_MORE			0x80 	// bit More for LPDU
#define LN_LAST			0x00    // bit Last for LPDU

#define MAX_LPDU_SIZE   (4096)//256		// maximum physical buffer size
#define MIN_LPDU_SIZE	16      // minimum physical buffer size

// Transport Layer
#define t_sb			0x80
#define t_rcv			0x81
#define create_t_c		0x82
#define c_t_c_reply		0x83
#define delete_t_c		0x84
#define d_t_c_reply		0x85
#define request_t_c		0x86
#define new_t_c			0x87
#define t_c_error		0x88
#define t_data_last		0xa0
#define t_data_more		0xa1
#define t_poll			0xa2    // customed t_data_last without data field

#define TCERR_NOTRANS	1		// error_code in t_c_error

#define TSB_DATA_AVAILABLE 	0x80 // t_sb(1)

#define MAXTPDUSIZE	16384	/* maximum size for a TPDU (t_data_last or t_data_more) */
#define T_DATA_HEADER 10	/* overhead in t_data_last/t_data_more before SPDU (tag + length_field + tcid) */

#define S_CREATE_T_C	3
#define S_C_T_C_REPLY	3
#define S_DELETE_T_C	3
#define S_D_T_C_REPLY	3
#define S_POLL			3
#define S_T_SB			4
#define S_T_RCV			3

#ifdef _DEBUG
#define TM_TRANS	60000 //60000 // 60000 //10000	/* Timeout Value in ms 							*/
#define TM_POLLING	100 //2000 //500 // 2000 //300		/* Polling Timeout in ms						*/
#else
#define TM_TRANS	60000 //60000 // 60000 //10000	/* Timeout Value in ms 							*/
#define TM_POLLING	100 //2000 //500 // 2000 //300		/* Polling Timeout in ms						*/
//#define TM_TRANS	300		/* Timeout Value in ms 							*/
//#define TM_POLLING	70		/* Polling Timeout in ms						*/
#endif

/* Session Layer */
#define dutag_open_session_request 		0x91
#define dutag_open_session_response 	0x92
#define dutag_create_session_request 	0x93
#define dutag_create_session_response 	0x94
#define dutag_close_session_request 	0x95
#define dutag_close_session_response 	0x96
#define dutag_session_number			0x90

#define RESOURCE_ID_SIZE	4		/* length of a resource identifier				*/

#define OPEN_SESSION_REQUEST_SIZE  6	//add by pmy
#define OPEN_SESSION_RESPONSE_SIZE  9	/* open_session_response size					*/
#define CLOSE_SESSION_REQUEST_SIZE	4	/* close_session_request size					*/
#define CLOSE_SESSION_RESPONSE_SIZE 5	/* dutag_close_session_response size					*/
#define SESSION_NUMBER_SIZE			4	/* dutag_session_number size wihtout data				*/

#ifdef _DEBUG
#define SESS_TIMEOUT	10000		/* Timeout Value in ms 							*/
#else
#define SESS_TIMEOUT	1000		/* Timeout Value in ms 							*/
#endif

/* session_status values */
#define SS_OK				0x00
#define SS_NOTALLOCATED		0xf0
#define SS_NOTAVAILABLE		0xf1
#define SS_BADVERSION		0xf2
#define SS_BUSY				0xf3


/* Transport Layer */
typedef struct
{
	unsigned char*	m_pbData;      // data_field of tpdu
	unsigned long 	m_len;        	// len is the length_field - 1 (without the byte reserved for tcid)
}xpdu_tdata_t;

typedef struct
{
	unsigned char m_SBflag;
}xpdu_tsb_t;

typedef struct
{
	unsigned char m_newTcid;
}xpdu_newtc_t;

typedef struct
{
	unsigned char m_errcode;
}xpdu_tcerror_t;

typedef struct
{
	unsigned char m_tcid;
	unsigned char m_status;
	unsigned long m_ressId;
}xpdu_open_session_request_t;

typedef struct
{
	unsigned long m_ressId;
	unsigned char m_status;
	unsigned char m_rtcid;
	unsigned short m_ssnb;
}xpdu_open_session_response_t;

typedef struct
{
	unsigned long m_ressId;
}xpdu_create_session_request_t;

typedef struct
{
	unsigned long m_ressId;
	unsigned char m_status;
	unsigned short m_ssnb;	   // second ssnb
}xpdu_create_session_response_t;

typedef struct
{
	unsigned char*	m_pbData;   	// data_field of session_number (APDU)
	unsigned long 	m_len;       	// len of the APDU
	unsigned short 	m_ssnb;	   	// sender ssnb
}xpdu_session_number_t;			//dutag_session_number  spdu

typedef struct
{
	unsigned short m_ssnb;
}xpdu_close_session_request_t;

typedef struct
{
	unsigned short 	m_ssnb;
	unsigned char 	m_status;
}xpdu_close_session_response_t;

typedef struct
{
	unsigned char	m_xpdu_tag;
	union
	{
		xpdu_tdata_t					m_xpdu_tdata;
		xpdu_tsb_t	  					m_xpdu_tsb;
		xpdu_newtc_t					m_xpdu_newtc;
		xpdu_tcerror_t					m_xpdu_tcerror;
		xpdu_open_session_request_t		m_xpdu_open_session_request;
		xpdu_open_session_response_t  	m_xpdu_open_session_response;
		xpdu_create_session_request_t 	m_xpdu_create_session_request;
		xpdu_create_session_response_t 	m_xpdu_create_session_response;
		xpdu_session_number_t   		m_xpdu_session_number;
		xpdu_close_session_request_t  	m_xpdu_close_session_request;
		xpdu_close_session_response_t 	m_xpdu_close_session_response;
	}m_du;
}xpdu_t;



/****************************************************************************
*
*	Application object tag values. Resource Direction:host<-->app
*
****************************************************************************/

#define CI_APDU_TAG_PROFILE_ENQ				0x9F8010		// resource mgr. <--->
#define CI_APDU_TAG_PROFILE					0x9F8011		// resource mgr. <--->
#define CI_APDU_TAG_PROFILE_CHANGE			0x9F8012		// resource mgr. <--->

#define CI_APDU_TAG_APPLICATION_INFO_ENG	0x9F8020		// application info. --->
#define CI_APDU_TAG_APPLICATION_INFO		0x9F8021		// application info. <---
#define CI_APDU_TAG_ENTER_MENU				0x9F8022		// application info. --->

#define CI_APDU_TAG_CA_INFO_ENQ				0x9F8030		// CA Support --->
#define CI_APDU_TAG_CA_INFO					0x9F8031		// CA Support <---
#define CI_APDU_TAG_CA_PMT					0x9F8032		// CA Support --->
#define CI_APDU_TAG_PMT_REPLY				0x9F8033		// CA Support <---

#define CI_APDU_TAG_TUNE					0x9F8400		// Host Control <---
#define CI_APDU_TAG_REPLACE					0x9F8401		// Host Control <---
#define CI_APDU_TAG_CLEAR_REPLACE			0x9F8402		// Host Control <---
#define CI_APDU_TAG_ASK_RELEASE				0x9F8403		// Host Control --->
#define CI_APDU_TAG_TUNE_BROADCAST_REQ      0x9F8404        // Host Control <---
#define CI_APDU_TAG_TUNE_REPLY              0x9F8405        // Host Control --->
#define CI_APDU_TAG_ASK_RELEASE_REPLY       0x9F8406        // Host Control <---

#define CI_APDU_TAG_DATE_TIME_ENQ			0x9F8440		// Date-time <---
#define CI_APDU_TAG_DATE_TIME				0x9F8441		// Date-time --->

#define CI_APDU_TAG_CLOSE_MMI				0x9F8800		// MMI --->
#define CI_APDU_TAG_DISPLAY_CONTROL			0x9F8801		// MMI <---
#define CI_APDU_TAG_DISPLAY_REPLY			0x9F8802		// MMI --->
#define CI_APDU_TAG_TEXT_LAST				0x9F8803		// MMI <---
#define CI_APDU_TAG_TEXT_MORE				0x9F8804		// MMI <---
#define CI_APDU_TAG_KEYPAD_CONTROL			0x9F8805		// MMI <---
#define CI_APDU_TAG_KEYPRESS				0x9F8806		// MMI --->
#define CI_APDU_TAG_ENQ						0x9F8807		// MMI <---
#define CI_APDU_TAG_ANSW					0x9F8808		// MMI --->
#define CI_APDU_TAG_MENU_LAST				0x9F8809		// MMI <---
#define CI_APDU_TAG_MENU_MORE				0x9F880A		// MMI <---
#define CI_APDU_TAG_MENU_ANSW				0x9F880B		// MMI --->
#define CI_APDU_TAG_LIST_LAST				0x9F880C		// MMI <---
#define CI_APDU_TAG_LIST_MORE				0x9F880D		// MMI <---
#define CI_APDU_TAG_SUBTITLE_SEGMENT_LAST	0x9F880E		// MMI <---
#define CI_APDU_TAG_SUBTITLE_SEGMENT_MORE	0x9F880F		// MMI --->
#define CI_APDU_TAG_DISPLAY_MESSAGE			0x9F8810		// MMI <---
#define CI_APDU_TAG_SCENE_END_MARK			0x9F8811		// MMI <---
#define CI_APDU_TAG_SCENE_DONE				0x9F8812		// MMI <---
#define CI_APDU_TAG_SCENE_CONTROL			0x9F8813		// MMI --->
#define CI_APDU_TAG_SUBTITLE_DOWNLOAD_LAST	0x9F8814		// MMI <---
#define CI_APDU_TAG_SUBTITLE_DOWNLOAD_MORE	0x9F8815		// MMI --->
#define CI_APDU_TAG_FLUSH_DOWNLOAD			0x9F8816		// MMI <---
#define CI_APDU_TAG_DOWNLOAD_REPLY			0x9F8817		// MMI <---

#define CI_APDU_TAG_REQUEST_START           0x9F8000        // APP MMI <---
#define CI_APDU_TAG_REQUEST_START_ACK       0x9F8001        // APP MMI --->
#define CI_APDU_TAG_FILE_REQUEST            0x9F8002        // APP MMI --->
#define CI_APDU_TAG_FILE_ACKNOWLEDGE        0x9F8003        // APP MMI <---
#define CI_APDU_TAG_APP_ABORT_REQUEST       0x9F8004        // APP MMI <--->
#define CI_APDU_TAG_APP_ABORT_ACK           0x9F8005        // APP MMI <--->

#define CI_APDU_TAG_COMMS_CMD				0x9F8C00		// low-speed comms. <---
#define CI_APDU_TAG_CONNECTION_DESCRIPTOR	0x9F8C01		// low-speed comms. <---
#define CI_APDU_TAG_COMMS_REPLY				0x9F8C02		// low-speed comms. --->
#define CI_APDU_TAG_COMMS_SEND_LAST			0x9F8C03		// low-speed comms. <---
#define CI_APDU_TAG_COMMS_SEND_MORE			0x9F8C04		// low-speed comms. <---
#define CI_APDU_TAG_COMMS_RCV_LAST			0x9F8C05		// low-speed comms. --->
#define CI_APDU_TAG_COMMS_RCV_MORE			0x9F8C06		// low-speed comms. --->

#define CI_APDU_EVENT_ENQUIRY				0x9F8F00		// EPG Future Evt Supp --->
#define CI_APDU_EVENT_REPLAY				0x9F8F01		// EPG Future Evt Supp <---

//COPY PROTECTION APDU
#define CI_APDU_CP_OPEN_REQ					0x9F9000		 // Evaluate Host support for CP <---
#define CI_APDU_CP_OPEN_CNF					0x9F9001         //  --->
#define CI_APDU_CP_DATA_REQ					0x9F9002         // Card & Host authentication dataAuthentication Key verificationCPKey generation <---
#define CI_APDU_CP_DATA_CNF					0x9F9003         //  --->
#define CI_APDU_CP_SYNC_REQ					0x9F9004         // Card-Host CPKey Synchronization <---
#define CI_APDU_CP_SYNC_CNF					0x9F9005         //  --->

// MMI modes
#define MMI_HIGH_LEVEL           0x01
#define MMI_OVERLAY_GRAPHICS     0x02
#define MMI_FULL_SCREEN 		 0x03

// Display Control Command
// Request that host enters the MMI mode indicated by the MMI_mode byte
#define DCC_SET_MMI_MODE						0x01
// Request that the host return a list of the character code tables that it can support
#define DCC_GET_DISPLAY_CHARACTER_TABLE_LIST	0x02
// Request that the host return a list of the character code tables that it can support
#define DCC_INPUT_CHARACTER_TABLE_LIST			0x03
// Request the profile of the display when used to display graphics overlaid over video.
#define DCC_GET_OVERLAY_CHARACTERISTICS			0x04
// Request the profile of the display when used to display graphics in replacement of video.
#define DCC_GET_FULL_SCREEN_CHARACTERISTICS		0x05

#define MMI_MODE_ACK		0x01

// Session status
#define SESSION_STATUS_NO_RSRC      0xF0   // Resource non-existent
#define SESSION_STATUS_RSRC_UNAVAIL 0xF1   // Resource unavailable
#define SESSION_STATUS_RSRC_VERSION 0xF2   // Resource has a lower version
#define SESSION_STATUS_RSRC_BUSY    0xF3   // Resource is busy


#endif	// !__CITAG_H
